package cn.qy.java.base.ch21;

/**
 * 打断 join
 */
public class Ch21_04_ThreadJoinInterrupt {
	
	public static void main(String[] args) {
		class Thread1 extends Thread {
			public void run() {
				System.out.println("Thread1 start");
				while(!this.isInterrupted()) {}
			};
		};
		
		Thread mainThread = Thread.currentThread();
		
		class Thread2 extends Thread {
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				// 中断主线程
				mainThread.interrupt();
				System.out.println("Thread2 end");
			};
		};
		
		Thread t1 = new Thread1();
		Thread t2 = new Thread2();

		t1.start();
		t2.start();
		
		try {
			t1.join();
		} catch (InterruptedException e) {
			System.out.println("main thread: " + e);

		}
		// interrupt 的使用
	    t1.interrupt();
		System.out.println("main thread end");
	}

}
